/*绑定回车到查询按钮*/
$(function(){
	$(document).keydown(function(){ 
		if(event.keyCode==13&&huichefs=='chaxun'){ 
		$("#le-search").trigger('click');
		return false;
		}
	})
});

//传值变量
var jinyongbz=0;
var mingcheng='';
var pageNumber=1;
//定位参数
var dwid='';
var dingwei='';
var dwpanduan=true;
//数据读取
function exeShowPage(){
	var totalNumber=0;
	var params={
			mingcheng:mingcheng,
			jinyongbz:jinyongbz,
			yibaoclbm:'',
			rukou:"1"
	};
	$.ajax({
        type:"POST",
        url:"./haocai/getCount",
        data:params,
        async:false,
        //请求成功完成后要执行的方法
        success: function(data){
    	totalNumber = data;
    	if(totalNumber==0){
			$("#a-table-list").css("display","none");
			$("#hcku-img").html("<img src='"+path+"/image/chaxtj/cxtj-nobody.png'>");
		}else{
			$("#a-table-list").css("display","block");
			$("#hcku-img").html("");
		}
        }
    });
	huicheflag=true;
    function getHaocai(name){
    	$('#kucunhc').html('');
    	var container = $('#pagination-'+name);
		container.pagination({
		dataSource:'./haocai/getHaocaiByPage?mingcheng='+encodeURI(mingcheng)+'&jinyongbz='+encodeURI(jinyongbz)
		+ '&yibaoclbm='+'&rukou=1',
		locator: '',
		pageSize: 12,
		pageNumber:pageNumber,
		totalNumber: totalNumber,
		showNavigator: true,
   	    formatNavigator: '共 <%= totalNumber %> 条',
		callback: function(data, pagination){
			var content = '';
			$(data).each(function(index,value){
        	 if(this.jinyongbz==1 ){
        		 var zt="启用";
             }
             else if(this.jinyongbz==0 ){
            	 var zt="禁用";
             }
        	 if(zt=="启用" ){
        		 var szt="禁用";
             }
             else if(zt=="禁用"){
            	 var szt="启用";
             }	
        	 if(this.dikucyjkg==1) {
        		var kcyj = "关闭预警";
        	}else {
        		var kcyj = "开启预警";
        	}
        	  if(this.chailingbz==0) {
        		var danwei = this.baozhuangdw;
        		var kucunsl = this.kucunsl+this.baozhuangdw;
        	}else{
        		var kucunsl = this.kucunsl+this.chailingdw;
        		var danwei = this.chailingdw;
        	}
        	  if(this.shengchancj==null) {
        		var shengchancj = "";
        	}else {
        		var shengchancj = this.shengchancj;
        	}
        	  var index=((pagination.pageNumber-1)*pagination.pageSize)+index+1;
        	content +='<tr id="hchang'+this.id+'">'
			    	+'<td width="6%"><label class=col-sm-12>'+index+'</label></td>'
			        +'<td width="15%" class="left">' +this.mingcheng+ "</td>" 
			        +'<td width="15%" class="left">' +this.baozhuanggg+"</td>" 
			        +'<td width="15%" class="left">' +shengchancj+"</td>" 
			        +'<td width="10%">' +kucunsl+'</td>'
			        +'<td width="18%"><a class="operation picikgkz" id="tiaozhenkc" style="display:none;"  data-toggle="modal" onclick="tiaozhengkucun('+this.id+','+this.kucunsl+',\''+danwei+'\','+2+',$(this))">调整库存</a>'
			        +'<a class="operation" id="yujingkq'+this.id+'"   data-toggle="modal" onclick="yujingkaiqi('+this.id+','+index+','+this.kucunxx+',\''+danwei+'\','+2+',$(this))">'+kcyj+'</a>'
			        +'<a class="operation" id="'+this.id+'" onclick="tiaozhuan('+this.id+','+pagination.pageNumber+')">详情</a>'
			        +'<a class="operation" id="qyjy'+this.id+'"   data-toggle="modal" onclick="jingyongqiyong('+pagination.pageNumber+','+this.id+','+index+','+2+',$(this))">'+zt+'</a>'
			        +'</tr>';
        			if(dwid&&dwid==this.id){
		        		dingwei=this.id;
		        	}
			 });
			  $('#kucunhc').html(content);
			  if(kucunpckg == true){
					$(".picikgkz").css("display","none");
				}else{
					$(".picikgkz").css("display","inline-block");
				}
			  if(dingwei!=null&&dingwei!=''){
				  $('#kucunhc').find('tr[id="hchang'+dingwei+'"]').addClass('list-tr');
			  }
			  if(!dwpanduan){
				  $('#kucunhc').find('tr[id="hchang'+dingwei+'"]').removeClass('list-tr');
			  }
			}
		});
		 return container;
	}
	getHaocai('fenyhc');
	if(totalNumber <= 12) {
		$('#pagination-fenyhc').css('display','none');
	}else{
		$('#pagination-fenyhc').css('display','block');
	}
}
function getCanshu(predwid,predwpageNo,predwming,predwzhuangtai){
	dwid=predwid;
	if(dwid!='null'){
	//页面显示
		jinyongbz=predwzhuangtai;
		pageNumber=predwpageNo;
		mingcheng=decodeURI(decodeURI(predwming));
		$('#hc-mingcheng').val(mingcheng);
		$('#hc-zhuangt').val(jinyongbz);
		if(jinyongbz==0){
			$('#hc-zhuangtaidiv').text('启用');
		}else if(jinyongbz==2){
			$('#hc-zhuangtaidiv').text('全部');
		}else if(jinyongbz==1){
			$('#hc-zhuangtaidiv').text('禁用');
		}
	}
	exeShowPage();
}

//查询按钮
$("#le-search").click(function(){
	mingcheng=$("#hc-mingcheng").val();
	jinyongbz=$("#hc-zhuangt").val();
	pageNumber=1;
	dwpanduan=false;
    exeShowPage();
});
//select模拟
$(".dropdown-menu li").click(function(){
	var bText = $(this).text();
	var bValue = $(this).val();
	$(this).parent().parent().find(".dropdown-toggle").text(bText);
	$(this).parent().parent().find("#hc-zhuangt").val(bValue);
	$(this).parent().css("display","none");
	mingcheng=$("#hc-mingcheng").val();
	jinyongbz=bValue;
	dwpanduan=false;
	pageNumber=1;
    exeShowPage();
 });
//查询条件重置按钮
$("#le-reset").click(function(){
	$("#hc-mingcheng").val("");
	$(".dropdown-toggle").text("启用")
	$("#hc-zhuangt").val(0);
	mingcheng=$("#hc-mingcheng").val();
	jinyongbz=0;
	dwpanduan=false;
	pageNumber=1;
    exeShowPage();
});
//添加按钮
$("#le-add").click(function(){
	$(this).css("color","#ffffff");
	$("#cedan").load(path+"haocai-add.jsp");
});


//进入耗材详情页面
function tiaozhuan(id,prepageNo){
	var dwpageNo=prepageNo;
	var dwming=$('#hc-mingcheng').val();
	var dwzhuangtai=$('#hc-zhuangt').val();
	dwming=encodeURI(encodeURI(dwming));
	$("#cedan").load(path+"/views/kucun/haocai-detail.jsp?ID="+id+"&dwming="+dwming+"&dwzhuangtai="+dwzhuangtai+"&dwpageNo="+dwpageNo);
}

$('#kuxunxx').keyup(function(){
	var val=$(this).val();
	var reg = /^[1-9]\d*$/ ;
	if(!reg.test(val)){
		notifyWarning("请输入正整数")
		$(this).val("");
	}
});
//判断库存批次是否开启
var kucunpckg;
function getYaopintjcs(prekucunpckg){
	kucunpckg = prekucunpckg;
}
//加.00
function getFloatStr(num){  
    if(num!=""){
    	num += ''; 
	    num = num.replace(/[^0-9|\.]/g, ''); //清除字符串中的非数字非.字符
	    if(/^0+/) //清除字符串开头的0
	        num = num.replace(/^0+/, '');
	    if(!/\./.test(num)) //为整数字符串在末尾添加.00
	        num += '.00000000';  
	    if(/^\./.test(num)) //字符以.开头时,在开头添加0
	        num = '0' + num;  
	    num += '00000000';        //在字符串末尾补零  
	    num = num.match(/\d+\.\d{8}/)[0];    
	    return num;
    }	
}; 
//处方价小数点后保存2位
$(document).on("focusout","[name='xiaoshoujia']",function(){
	var jine = $(this).val();
	   jine = getFloatStr(jine);
	   $(this).val(jine);
});